package com.example.multitask;

import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.concurrent.*;

public class MultiUtil {

    public static void main(String[] args) {

        //创建使用固定线程数的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(45);
        //创建监控上面各个线程的线程
        CountDownLatch countDownLatch = new CountDownLatch(45);

        //47个指标执行成功失败标记
        ArrayList<Future<String>> subTaskResults = new ArrayList<>();


        for (int i = 1; i < 51; i++) {
            if(i == 43 || i ==44 || i == 47|| i == 48|| i == 49) continue;
            int finalI = i;
            Callable<String> callable = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    countDownLatch.countDown();
                    System.out.println("call "+finalI);
                    return "返回的结果："+finalI;
                }
            };


            Future<String> submit = threadPool.submit(callable);//有返回值
            subTaskResults.add(submit);
        }
        //for循环结束


        //等到47个线程执行完毕后关闭
        threadPool.shutdown();
        try {
            //等47个线程都countdown 才执行后面内容
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (Future<String> future:subTaskResults){
            try {
                String s = future.get();
                System.out.println(s);


            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }



    }
}
